var api = require('../../utils/api.js')
var Ajax = require("../../utils/ajax.js")
var Modal = require("../../utils/modal.js")
var app = getApp()
Page({
  data: {
  },
  onLoad(e) {
    var self = this;
    this.data.grantid = e.grantid;
    this.data.avatarurl = e.avatarurl;
    Ajax.AsyncPost('/grant/add_grant_member/', { B_id: e.grantid, avatarurl: e.avatarurl }, (data) => {
      self.data.meshs = []
      var meshs = app.globalData.meshs;
      for (let i = 0; i < meshs.length; i++) {
        self.data.meshs.push({ name: meshs[i].name, meshid: meshs[i].id, choosen: false });
      }
      self.setData({ meshs: self.data.meshs });
    })
  },

  choose: function (e) {
    var index = e.currentTarget.dataset.index;
    var temp_str_choosen = 'meshs[' + index + '].choosen';
    if (this.data.meshs[index].choosen) {
      this.setData({ [temp_str_choosen]: false });
    } else {
      this.setData({ [temp_str_choosen]: true });
    }
  },
  grant(e) {
    var self = this;
    Modal.showModal("确定将选中网络管理权限授予该用户？", () => {
      var data = []
      var meshs = self.data.meshs;
      for (var i = 0; i < meshs.length; i++) {
        if (meshs[i].choosen)
          data.push(meshs[i].meshid)
      }
      Ajax.AsyncPost('/grant/add_mesh_to_member_first/', {
        B_id: self.data.grantid,
        mesh_id_list: data,
      }, (data) => {
        wx.navigateBack();
      })
    })
  }
})
